Skip to content

Conversation

@westey-m
Copy link
Contributor

@westey-m westey-m commented Dec 3, 2025

Motivation and Context

#2518
#2054

Description

  • Refactor ChatMessageStore interface to be more similar to AIContextProvider, to give more certainty as to the data being passed to it, and the circumstances under which that data is passed.
  • Add a ChatMessageStore decorator that makes it easy to filter the messages being passed to the ChatMessageStore. This allows us to easily avoid persisting messages from AIContextProviders in the chat history.
AIAgent agent = azureOpenAIClient
    .GetChatClient(deploymentName)
    .CreateAIAgent(new ChatClientAgentOptions
    {
        ChatOptions = new() { Instructions = "You are a helpful support specialist for Contoso Outdoors. Answer questions using the provided context and cite the source document when available." },
        AIContextProviderFactory = ctx => new TextSearchProvider(SearchAdapter, ctx.SerializedState, ctx.JsonSerializerOptions, textSearchOptions),
        // Since we are using ChatCompletion which stores chat history locally, we can also add a message removal policy
        // that removes messages produced by the TextSearchProvider before they are added to the chat history, so that
        // we don't bloat chat history with all the search result messages.
        ChatMessageStoreFactory = ctx => new InMemoryChatMessageStore(ctx.SerializedState, ctx.JsonSerializerOptions)
            .WithAIContextProviderMessageRemoval(),
    });```

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows the [Contribution Guidelines](https://github.com/microsoft/agent-framework/blob/main/CONTRIBUTING.md)
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] **Is this a breaking change?** If yes, add "[BREAKING]" prefix to the title of the PR.

@markwallace-microsoft markwallace-microsoft added .NET workflows Related to Workflows in agent-framework labels Dec 3, 2025
@github-actions github-actions bot changed the title Refactor ChatMessageStore methods to be similar to AIContextProvider .NET: Refactor ChatMessageStore methods to be similar to AIContextProvider Dec 3, 2025
@westey-m westey-m changed the title .NET: Refactor ChatMessageStore methods to be similar to AIContextProvider .NET: [BREAKING] Refactor ChatMessageStore methods to be similar to AIContextProvider Dec 5, 2025
@westey-m westey-m changed the title .NET: [BREAKING] Refactor ChatMessageStore methods to be similar to AIContextProvider .NET: [BREAKING] Refactor ChatMessageStore methods to be similar to AIContextProvider and add filtering support Dec 5, 2025
@westey-m westey-m marked this pull request as ready for review December 5, 2025 18:59
Copilot AI review requested due to automatic review settings December 5, 2025 18:59
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a breaking refactoring of the ChatMessageStore interface to align with the AIContextProvider pattern, providing more explicit control over when and how messages are persisted. The changes replace the generic GetMessagesAsync and AddMessagesAsync methods with lifecycle-aware InvokingAsync and InvokedAsync methods that receive rich context about agent invocations.

Key changes:

  • Replaced GetMessagesAsync() with InvokingAsync(InvokingContext) to provide context about the current invocation when retrieving messages
  • Replaced AddMessagesAsync(messages) with InvokedAsync(InvokedContext) to provide detailed context including request messages, AI context provider messages, response messages, and any exceptions
  • Added ChatMessageStoreMessageFilter decorator to enable filtering of messages before they are stored or retrieved
  • Added extension methods WithMessageFilters() and WithAIContextProviderMessageRemoval() to simplify common filtering scenarios

Reviewed changes

Copilot reviewed 21 out of 21 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
dotnet/src/Microsoft.Agents.AI.Abstractions/ChatMessageStore.cs Core interface refactoring with new InvokingAsync and InvokedAsync methods, plus new context classes InvokingContext and InvokedContext
dotnet/src/Microsoft.Agents.AI.Abstractions/ChatMessageStoreExtensions.cs New extension methods for adding message filters and AI context provider message removal
dotnet/src/Microsoft.Agents.AI.Abstractions/ChatMessageStoreMessageFilter.cs New decorator class that wraps a ChatMessageStore to enable filtering messages
dotnet/src/Microsoft.Agents.AI.Abstractions/InMemoryChatMessageStore.cs Updated implementation to use new interface methods
dotnet/src/Microsoft.Agents.AI.CosmosNoSql/CosmosChatMessageStore.cs Updated implementation to use new interface methods with Cosmos DB storage
dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowMessageStore.cs Updated implementation to use new interface methods for workflow scenarios
dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowHostAgent.cs Updated to use internal AddMessages method directly instead of the refactored interface methods
dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs Updated to call InvokingAsync and InvokedAsync with proper context, tracking message store messages separately
dotnet/samples/GettingStarted/Agents/Agent_Step07_3rdPartyThreadStorage/Program.cs Updated sample to implement new interface methods
dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step01_BasicTextRAG/Program.cs Added example of using WithAIContextProviderMessageRemoval() to filter search result messages
dotnet/samples/GettingStarted/AgentProviders/Agent_With_CustomImplementation/Program.cs Updated custom agent implementation to use new interface methods
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/ChatMessageStoreTests.cs Updated base tests to use new interface methods
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/InMemoryChatMessageStoreTests.cs Updated tests to verify new interface behavior
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/ChatMessageStoreMessageFilterTests.cs New test file with comprehensive coverage for the filter decorator
dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/CosmosChatMessageStoreTests.cs Updated tests to use new interface methods with Cosmos DB
dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentTests.cs Updated unit tests to verify new interface integration
dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgent_BackgroundResponsesTests.cs Updated tests for background response scenarios
Integration test fixtures Updated all integration test fixtures to use InvokingAsync for retrieving messages

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

.NET workflows Related to Workflows in agent-framework

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants